package bmaso.purest.castor;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.ValidationException;

public class MarshalTag extends BodyTagSupport {
	
	private PageContext pc;
	private String mappingResourceName = null;
	private Object toMarshal = null;
	
	@Override
	public int doEndTag() throws JspException {
		try {
			if(getMappingResource() == null) {
				Marshaller.marshal(toMarshal, pc.getOut());
			}
			
			return SKIP_BODY;
		} catch(MarshalException me) {
			throw new JspException(me);
		} catch(ValidationException ve) {
			throw new JspException(ve);
		}
	}
	
	public void setPageContext(PageContext pc) {
		super.setPageContext(pc);
		this.pc = pc;
	}
	
	public void setMappingResource(String mappingResourceName) {
		this.mappingResourceName = mappingResourceName;
	}
	
	public String getMappingResource() {
		return mappingResourceName;
	}
	
	public void setObject(Object toMarshal) {
		this.toMarshal = toMarshal;
	}
	
	public Object getObject() {
		return toMarshal;
	}
}
